import { message } from 'antd';
import axios from 'axios';
import { getUserToken } from '../utils/user-token';
const instance = axios.create({
  timeout: 10 * 1000,
});
instance.interceptors.request.use(
  (config) => {
    config.headers['Authorization'] = `Bearer ${getUserToken()}`;
    return config;
  },
  (error) => Promise.reject(error),
);
instance.interceptors.response.use((res) => {
  console.log('res', res);
  const resData = res.data || {};
  const { errno, data, msg } = resData;
  if (errno !== 0) {
    if (msg) {
      message.error(msg);
    }
    throw new Error(msg);
  }
  return data;
});
export default instance;

export type ResType = {
  errno: number;
  data?: ResDataType;
  msg?: string;
};

export type ResDataType = {
  [key: string]: any;
};
